<!--
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->
<div *ngIf="_registry" class="resource-header">
    <div class="resource-header-left">
        <h1 *ngIf="!isHistorian()">{{_registry.name}}</h1>
    </div>
    <div *ngIf="!isHistorian()">
        <button type="button" class="registry" (click)="openNewResourceModal()">
            <span>+ Create New {{_registry.registryType}}</span>
        </button>
    </div>
</div>
<div *ngIf="_registry" class="resource-list" [ngClass]="{'transaction-list' : isHistorian()}">
    <div *ngIf="!isHistorian()">
        <div class="title" [ngClass]="{'resource-empty' : !resources || resources.length === 0}">
            <div class="id">ID</div>
            <div class="data">Data</div>
        </div>
        <div *ngFor="let resource of resources" class="resource-container">
            <div checkOverFlow [changed]="resource.getIdentifier()"
                 [expanded]="resource.getIdentifier() === expandedResource"
                 (hasOverFlow)="hasOverFlow($event, resource)"
                 class="resource-content" [ngClass]="{'expanded' : resource.getIdentifier() === expandedResource}">
                <div class="id">{{resource.getIdentifier()}}</div>
                <pre #resourcedata
                     [ngClass]="{'gradient' : resource.getIdentifier() !== expandedResource && overFlowedResources[resource.getIdentifier()], 'tiny-gradient' : resource.getIdentifier() === expandedResource || !overFlowedResources[resource.getIdentifier()]}"
                     class="data">{{serialize(resource)}}</pre>
                <div lass="resource-icon">
                    <button type="button" class="icon edit-resource" (click)="editResource(resource)">
                        <svg class="ibm-icon" aria-hidden="true">
                            <use xlink:href="#icon-edit_32"></use>
                        </svg>
                    </button>
                </div>
                <div class="resource-icon">
                    <button type="button" class="icon delete-resource" (click)="openDeleteResourceModal(resource)">
                        <svg class="ibm-icon" aria-hidden="true">
                            <use xlink:href="#icon-bin_icon"></use>
                        </svg>
                    </button>
                </div>
            </div>
            <button *ngIf="overFlowedResources[resource.getIdentifier()]" class="expand" type="button"
                    (click)="expandResource(resource)">
                {{resource.getIdentifier() === expandedResource ? 'Collapse' : 'Show All'}}
            </button>
        </div>
    </div>

    <table *ngIf="resources && resources.length > 0 && isHistorian()">
        <thead [ngClass]="{'tbody-scrolled' : tableScrolled}">
        <tr>
            <th class="normal-column">Date, Time</th>
            <th class="normal-column">Entry Type</th>
            <th class="normal-column">Participant</th>
            <th class="narrow-column"></th>
        </tr>
        </thead>
        <tbody id="transactiontbody" checkScroll (hasScroll)="updateTableScroll($event)">
        <tr *ngFor="let resource of resources">
            <td class="normal-column truncate-text">{{ resource.transactionTimestamp | date:'y-MM-dd, HH:mm:ss' }}</td>
            <td class="normal-column truncate-text"><span placement="top" container="body" ngbTooltip="{{resource.transactionType || null}}">{{ resource.transactionType ? resource.transactionType.substring(resource.transactionType.lastIndexOf('.') + 1) : 'TBC' }}</span></td>
            <td class="normal-column truncate-text"><span placement="top" container="body" ngbTooltip="{{resource.participantInvoking ? resource.participantInvoking.$namespace + '.' + resource.participantInvoking.$type : null}}">{{ resource.participantInvoking ?
                resource.participantInvoking.$identifier + ' (' + resource.participantInvoking.$type + ')'  : 'none' }}</span>
            </td>
            <td class="narrow-column"><a href="#" (click)="viewTransactionData(resource)">view record</a></td>
        </tr>
        </tbody>
    </table>

    <div *ngIf="!resources || resources.length === 0" class="no-resources">
        <svg class="ibm-icon" aria-hidden="true">
            <use xlink:href="#icon-Emptiness"></use>
        </svg>
        <h3>This registry is empty!</h3>
        <p *ngIf="!isHistorian()">To create resources in this registry click create new at the
            top of
            this page</p>
        <p *ngIf="isHistorian()">Submit a Transaction via the side menu</p>
    </div>
</div>
